﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CustomerAddressTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("city", "address1", "address2",
            "contactName", "postalCode", "phoneNumber",
            "faxNumber", "email")]
        public void Can_CRUD(string city,
            string address1,
            string address2,
            string contactName,
            string postalCode,
            string phoneNumber,
            string faxNumber,
            string email)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var customer = Processor.Execute(new CreateCustomer
            {
                AccountID = account.EntityID
            }).Result;

            #endregion

            var address = Processor.Execute(new CreateCustomerAddress
            {
                HolderID = customer.EntityID,
                CountryID = country.EntityID,
                RegionID = localizationProfile.Region.EntityID,
                City = city,
                Address1 = address1,
                Address2 = address2,
                ContactName = contactName,
                PostalCode = postalCode,
                PhoneNumber = phoneNumber,
                FaxNumber = faxNumber,
                Email = email,
                Flags = CustomerAddressFlags.None
            }).Result;

            Assert.NotNull(address);
            address.City.AreEqualIgnoringCase(city);
            address.Address1.AreEqualIgnoringCase(address1);
            address.Address2.AreEqualIgnoringCase(address2);
            address.ContactName.AreEqualIgnoringCase(contactName);
            address.PostalCode.AreEqualIgnoringCase(postalCode);
            address.PhoneNumber.AreEqualIgnoringCase(phoneNumber);
            address.FaxNumber.AreEqualIgnoringCase(faxNumber);
            address.Email.AreEqualIgnoringCase(email);
            address.Flags.AreEqual(CustomerAddressFlags.None);
            address.AreEqual(Processor.Execute(new GetCustomerAddress
            {
                EntityID = address.EntityID
            }).Result);

            city = city.Reverse();
            address1 = address1.Reverse();
            address2 = address2.Reverse();
            contactName = contactName.Reverse();
            postalCode = postalCode.Reverse();
            phoneNumber = phoneNumber.Reverse();
            faxNumber = faxNumber.Reverse();
            email = email.Reverse();
            var upd_address = Processor.Execute(new UpdateCustomerAddress
            {
                EntityID = address.EntityID,
                CountryID = country.EntityID,
                RegionID = localizationProfile.Region.EntityID,
                City = city,
                Address1 = address1,
                Address2 = address2,
                ContactName = contactName,
                PostalCode = postalCode,
                PhoneNumber = phoneNumber,
                FaxNumber = faxNumber,
                Email = email,
                Flags = CustomerAddressFlags.Active
            }).Result;

            Assert.NotNull(upd_address);
            upd_address.AreEqual(address);
            upd_address.City.AreEqualIgnoringCase(city);
            upd_address.Address1.AreEqualIgnoringCase(address1);
            upd_address.Address2.AreEqualIgnoringCase(address2);
            upd_address.ContactName.AreEqualIgnoringCase(contactName);
            upd_address.PostalCode.AreEqualIgnoringCase(postalCode);
            upd_address.PhoneNumber.AreEqualIgnoringCase(phoneNumber);
            upd_address.FaxNumber.AreEqualIgnoringCase(faxNumber);
            upd_address.Email.AreEqualIgnoringCase(email);
            upd_address.Flags.AreEqual(CustomerAddressFlags.Active);

            Processor.Execute(new DeleteCustomerAddress
            {
                EntityID = address.EntityID
            });
        }
    }
}
